﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;

namespace StockHelper.Scripts.Sound
{
    class Sound
    {
        private static WaveOutEvent outputDevice = new WaveOutEvent();
        private static AudioFileReader audioFile;
        private static List<string> playList = new List<string>();

        private static Thread thread;

        /// <summary>
        /// 音量 0~1
        /// </summary>
        public static float Volume
        {
            set {
                outputDevice.Volume = value;
            }
            get {
                return outputDevice.Volume;
            }
        }


        public static void Play(string sound)
        {
            playList.Clear();
            playList.Add(sound);
            if (thread != null) return;
            thread = new Thread(new ThreadStart(PlayList));
            thread.Start();
        }

        public static void Play(List<string> sound)
        {
            foreach (string a in sound)
                playList.Add(a);
            //调用test实例的MyThread方法
            if (thread != null) return;
            thread = new Thread(new ThreadStart(PlayList));
            //启动线程
            thread.Start();
        }

        public static void Stop() {
            outputDevice.Stop();
            if (thread != null) {
                thread.Abort();
                thread = null;
            }
            playList.Clear();
        }



        private static void PlayList()
        {
            while (playList.Count > 0)
            {
                string path = playList[0].Replace('/', '\\');
                audioFile = new AudioFileReader(path);
                outputDevice.Init(audioFile);
                outputDevice.Play();
                while (outputDevice.PlaybackState != PlaybackState.Stopped)
                {
                    Thread.Sleep(10);
                }
                playList.RemoveAt(0);
            }
            thread = null;
        }
        


    }
}
